package com.aw.baselib.base;

import android.app.Activity;
import android.app.Application;
import android.graphics.Typeface;
import android.os.Bundle;


import com.aw.baselib.util.AwLog;
import com.aw.baselib.util.AwNetWatchdog;
import com.aw.baselib.util.update.AwFileDownloadUtil;

import java.util.HashMap;

import okhttp3.Call;

/**
 * Created by hzw
 */

public abstract class AwBaseApplication extends Application {

    private static AwBaseApplication instance;
    public static HashMap<String, Call> downCalls = new HashMap<>();
    public static AwNetWatchdog netWatchdog;
    public static Activity currentActivity;
    public static boolean isForeground;
    private int activityCount;
    protected Typeface typefaceRegular, typefaceMedium, typefaceLight, typefaceBold;

    public static AwBaseApplication getInstance() {
        return instance;
    }

    @Override
    public void onCreate() {
        super.onCreate();
        instance = this;
        checkNetwork();
        initTypeface();
        this.registerActivityLifecycleCallbacks(new Application.ActivityLifecycleCallbacks() {
            @Override
            public void onActivityCreated(Activity activity, Bundle savedInstanceState) {
                //                AwLog.d("===> onActivityCreated: " + activity.getClass().getName());
            }

            @Override
            public void onActivityStarted(Activity activity) {
                activityCount++;
                AwLog.d("===> onActivityStarted: " + activity.getClass().getName());
                currentActivity = activity;
                setIsForeground();
            }

            @Override
            public void onActivityResumed(Activity activity) {

            }

            @Override
            public void onActivityPaused(Activity activity) {

            }

            @Override
            public void onActivityStopped(Activity activity) {
                activityCount--;
                setIsForeground();
            }

            @Override
            public void onActivitySaveInstanceState(Activity activity, Bundle outState) {

            }

            @Override
            public void onActivityDestroyed(Activity activity) {

            }
        });
    }

    protected abstract void needReLogin();
    protected abstract boolean isAllowShowReloginDialog();

    private void checkNetwork() {
        netWatchdog = new AwNetWatchdog(this);
        netWatchdog.setNetConnectedListener(new AwNetWatchdog.NetConnectedListener() {

            @Override
            public void onReNetConnected(boolean isReconnect) {

            }

            @Override
            public void onNetUnConnected() {
                if(AwBaseApplication.downCalls == null || AwBaseApplication.downCalls.size() == 0) {
                    return;
                }
                for (String key : AwBaseApplication.downCalls.keySet()) {
                    AwFileDownloadUtil.get().cancel(instance, key);
                }
                //                showDialogToFinish(getString(R.string.common_network_offline));
            }
        });
        netWatchdog.startWatch();
    }

    public void cancelNetWatchdog() {
        if(netWatchdog != null) {
            netWatchdog.stopWatch();
        }
    }

    protected void initTypeface() {
        typefaceRegular = Typeface.createFromAsset(getAssets(), "fonts/Montserrat-Regular.ttf");
        typefaceMedium = Typeface.createFromAsset(getAssets(), "fonts/Montserrat-Medium.ttf");
        typefaceLight = Typeface.createFromAsset(getAssets(), "fonts/Montserrat-Light.ttf");
        typefaceBold = Typeface.createFromAsset(getAssets(), "fonts/Montserrat-SemiBold.ttf");

//        typefaceRegular = Typeface.create("sans-serif", Typeface.NORMAL);
//        typefaceMedium = Typeface.create("sans-serif-medium", Typeface.NORMAL);
//        typefaceLight = Typeface.createFromAsset(getAssets(), "fonts/Alibaba-PuHuiTi-Light.otf");
//        typefaceBold = Typeface.create("sans-serif-medium", Typeface.BOLD);
    }

    public Typeface getTypefaceRegular() {
        return typefaceRegular;
    }

    public Typeface getTypefaceMedium() {
        return typefaceMedium;
    }

    public Typeface getTypefaceLight() {
        return typefaceLight;
    }

    public Typeface getTypefaceBold() {
        return typefaceBold;
    }

    protected void setIsForeground() {
        if (activityCount > 0) {
            isForeground = true;
        } else {
            isForeground = false;
        }
    }


}
